#!/usr/bin/python2.5
# -*- coding: cp1250 -*-
#
# Copyright 2008 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""
kindly.cz application

admin tools
"""

__author__ = 'Nikola Tepper'

from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext.webapp import template
from google.appengine.api import taskqueue

import string
import settings
import timezones 

from models import *
from datetime import *        
from basehandler import *

class Emails(BaseHandler):
    def get(self, encoded_name):  
        
        emails = "" 
        title = "All user emails"  
        
        if encoded_name[1:] == "":   
            # show all emails            
            query = UserSettings.all()
            settings = query.fetch(10000)
            i = 0
            for setting in settings:
                i += 1
                if i == 300:
                    emails = emails + "<br><br>---------------<br><br>"
                    i = 0                
                emails = emails + setting.user.email() + "; "                               
        else:
            # use key to retrieve periodical
            periodical_name = encoded_name.lower().replace("%20", " ")
            periodical = Periodical.get_by_key_name(periodical_name)
            
            if periodical:                 
                title = "Subscriber emails for " + periodical.name
                query = Subscription.all()  
                query.ancestor(periodical)
                subscriptions = query.fetch(10000)
                i = 0
                for subscription in subscriptions:
                    i += 1
                    if i == 300:
                        emails = emails + "<br><br>---------------<br><br>"
                        i = 0
                    emails = emails + subscription.user_settings.user.email() + "; "
            else:
                title = "Periodical not found!"
                
        query = Periodical.all()
        periodicals = query.fetch(10000)
        for periodical in periodicals:
            setattr(periodical, "keyname", periodical.key().name().replace(" ", "%20"))  
            
        self.render_to_response('a_emails.html', 
                                {'title': title,
                                 'emails': emails,
                                 'periodicals': periodicals})
        
        
class AddBook(BaseHandler):
    def get(self, encoded_name):  
        
        book_name = encoded_name.lower().replace("%20", " ")
        book = Book.get_by_key_name(book_name)
        
        if not book:                 
            logging.debug("Adding book %s" % book_name)            
            book = Book.get_or_insert(key_name = book_name, 
                                      name = encoded_name,
                                      filename = encoded_name)  
            
            self.response.out.write("Added book %s" % book_name)  
    
def main():
    url_map = [('/_a/emails/(.*)', Emails),
               ('/_a/addbook/(.*)', AddBook)]
    
    application = webapp.WSGIApplication(url_map, debug=True)
    run_wsgi_app(application)

if __name__ == '__main__':
    main()